package de.tabuma.lib.webservice.wsdl;

import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
/**
 *
 * @author alefen
 */
public class PortTypeFacade {

    private static final Logger log = Logger.getLogger(PortTypeFacade.class.getName());

    public static String getPortType(Hashtable<String,Class> methodTable) {
        String str = "";
        Enumeration<String> keys = methodTable.keys();
        while(keys.hasMoreElements()) {
            String key = keys.nextElement();
            Class clazz = methodTable.get(key);
            str += "<portType name=\""+key+"PortType\">\n";
            for(int i=0;i<clazz.getDeclaredMethods().length;i++) {
                str += getOperation(key, clazz.getDeclaredMethods()[i]);
            }
            str += "</portType>\n";
        }
        return str;
    }

    public static String getOperation(String name, Method method) {
        String str = "";
        str += "<operation name=\""+name+"_"+method.getName()+"\">\n";
        str += "<input name=\""+name+"_"+method.getName()+"_input\" message=\"tns:"+name+"_"+method.getName()+"_input\"/>\n";
        if(!method.getReturnType().getName().equals("void")) {
            str += "<output name=\""+name+"_"+method.getName()+"_output\" message=\"tns:"+name+"_"+method.getName()+"_output\"/>\n";
            str += "<fault name=\""+name+"_"+method.getName()+"_exception\" message=\"tns:"+name+"_"+method.getName()+"_exception\"/>\n";
        }
        str += "</operation>\n";
        return str;
    }
}
